<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\services\message\service;


use app\dao\message\service\StoreServiceRecordDao;
use app\services\BaseServices;
use app\services\user\UserServices;
use crmeb\traits\ServicesTrait;
use think\facade\Db;

/**
 * Class StoreServiceRecordServices
 * @package app\services\message\service
 * @mixin StoreServiceRecordDao
 */
class StoreServiceRecordServices extends BaseServices
{

    use ServicesTrait;

    /**
     * StoreServiceRecordServices constructor.
     * @param StoreServiceRecordDao $dao
     */
    public function __construct(StoreServiceRecordDao $dao)
    {
        $this->dao = $dao;
    }

	/**
	 * 获取客服用户聊天列表
	 * @param int $userId
	 * @param string $nickname
	 * @param int $isTourist
	 * @return array
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
    public function getServiceList(int $userId, string $nickname = '', int $isTourist = 0)
    {
        [$page, $limit] = $this->getPageValue();
        $list = $this->dao->getServiceList(['user_id' => $userId, 'title' => $nickname, 'is_tourist' => $isTourist], $page, $limit, ['user', 'service'],'sticky desc,update_time desc');
        foreach ($list as &$item) {
            if ($item['message_type'] == 1) {
                $item['message'] = substrUTf8($item['message'], '10', 'UTF-8', '');
            }
            if (isset($item['kefu_nickname']) && $item['kefu_nickname']) {
                $item['nickname'] = $item['kefu_nickname'];
            }
            if (isset($item['wx_nickname']) && $item['wx_nickname'] && !$item['nickname']) {
                $item['nickname'] = $item['wx_nickname'];
            }
            if (isset($item['kefu_avatar']) && $item['kefu_avatar']) {
                $item['avatar'] = $item['kefu_avatar'];
            }
            if (isset($item['wx_avatar']) && $item['wx_avatar'] && !$item['avatar']) {
                $item['avatar'] = $item['wx_avatar'];
            }
            $item['_update_time'] = date('Y-m-d H:i', $item['update_time']);
        }
        return $list;
    }

    /**
     * 更新客服用户信息
     * @param int $uid
     * @param array $data
     * @return mixed
     */
    public function updateRecord(array $where, array $data)
    {
        return $this->dao->update($where, $data);
    }
	public function delRecord(int $id)
	{
		return $this->dao->delete($id);
	}
	/**
	 * 更新客服用户信息
	 * @param int $uid
	 * @param array $data
	 * @return mixed
	 */
	public function user_count(int $id)
	{
		return $this->dao->sum(['user_id' => $id],'mssage_num');
	}
	public function setAllRead(int $uid)
	{
		$this->dao->update(['user_id' => $uid], ['mssage_num' => 0]);
        DB::name('store_service_log')->where(['to_uid' => $uid])->update(['type' => 1]);

	}

	public function saveRecord(int $uid, int $toUid, string $message, int $type, int $messageType, int $num, int $isTourist = 0, string $nickname = '', string $avatar = '')
	{

		$info = $this->dao->get(['user_id' => $uid, 'to_uid' => $toUid]);
		/** @var UserServices $userService */
		$userService = app()->make(UserServices::class);
		$_userInfo = $userService->getUserInfo($toUid, 'nickname,avatar');
		if ($info) {
			Db::name('store_service_record')->where(['user_id' => $uid, 'to_uid' => $toUid])->where('id','<>', $info['id'])->delete();

			$info->type = $type;
			$info->message = $message;
			$info->message_type = $messageType;
			$info->update_time = time();
			$info->mssage_num = 0;
			if ($avatar) $info->avatar = $_userInfo['avatar'];
			if ($nickname) $info->nickname = $_userInfo['nickname'];
			$info->save();
			return $info->toArray();
		} else {
			$this->dao->delete (['user_id' => $uid, 'to_uid' => $toUid]);

			return $this->dao->save([
				'user_id' => $uid,
				'to_uid' => $toUid,
				'type' => $type,
				'message' => $message,
				'avatar' =>$_userInfo['avatar'],
				'nickname' =>$_userInfo['nickname'],
				'message_type' => $messageType,
				'mssage_num' => 0,
				'add_time' => time(),
				'update_time' => time(),
				'is_tourist' => $isTourist
			])->toArray();
		}
	}
	public function saveRecords(int $uid, int $toUid, string $message, int $type, int $messageType, int $num, int $isTourist = 0, string $nickname = '', string $avatar = '')
	{
		$info = $this->dao->get(['user_id' => $toUid, 'to_uid' => $uid]);

		if (!$info) {
			$this->dao->delete (['user_id' => $toUid, 'to_uid' => $uid]);
			return $this->dao->save([
				'user_id' => $toUid,
				'to_uid' => $uid,
				'type' => $type,
				'message' => $message,
				'avatar' =>$avatar,
				'nickname' =>$nickname,
				'message_type' => $messageType,
				'mssage_num' => $num,
				'add_time' => time(),
				'update_time' => time(),
				'is_tourist' => 0
			])->toArray();
		}else{
			Db::name('store_service_record')->where(['user_id' => $toUid, 'to_uid' => $uid])->where('id','<>', $info['id'])->delete();
			$info->type = $type;
			$info->message = $message;
			$info->message_type = $messageType;
			$info->update_time = time();
			$info->mssage_num = $num;
			if ($info->avatar) $info->avatar = $avatar;
			if ($info->avatar) $info->nickname = $nickname;
			$info->save();
			return $info->toArray();

		}


	}

//	public function saveRecord(int $uid, int $toUid, string $message, int $type, int $messageType, int $num, int $isTourist = 0, string $nickname = '', string $avatar = '')
//	{
//		$info = $this->dao->get(['user_id' => $toUid, 'to_uid' => $uid]);
//		if ($info) {
//			$info->type = $type;
//			$info->message = $message;
//			$info->message_type = $messageType;
//			$info->update_time = time();
//			$info->mssage_num = $num;
//			if ($avatar) $info->avatar = $avatar;
//			if ($nickname) $info->nickname = $nickname;
//			$info->save();
//			$this->dao->update(['user_id' => $uid, 'to_uid' => $toUid], ['message' => $message, 'message_type' => $messageType]);
//			return $info->toArray();
//		} else {
//			return $this->dao->save([
//				'user_id' => $toUid,
//				'to_uid' => $uid,
//				'type' => $type,
//				'message' => $message,
//				'avatar' => $avatar,
//				'nickname' => $nickname,
//				'message_type' => $messageType,
//				'mssage_num' => $num,
//				'add_time' => time(),
//				'update_time' => time(),
//				'is_tourist' => $isTourist
//			])->toArray();
//		}
//	}
}
